<?php
/**
 * 监控服务 ws http 8811端口
 */

 class Server {
    const PORT = 8811;

    public function port()
    {
        $shell = "netstat -anp 2>/dev/null | grep " . self::PORT . " | grep LISTEN " . "| wc -l ";
        $result = shell_exec($shell);
//        echo $result;
        if($result != 1){
//            发送报警服务，说明挂掉了
            echo date('Ymd H:i:s')."error".PHP_EOL;
        }else{
            echo date('Ymd H:i:s')."success".PHP_EOL;
        }
    }
 }

 //每隔两秒执行
swoole_timer_tick(2000,function (){
    (new Server())->port();
    echo "time-start" . PHP_EOL;
});

 /**
  * linux有个命令可以将如果有输出内容的，导出到指定路径
  *
  * nohup php server.php > /home/vagrant/Code/thinkphp/script/monitor/server_log.txt
  */
